package com.fbs.job.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author wwd
 * @title: DateTool
 * @projectName housesale
 * @description: TODO
 * @date 2022/3/716:33
 */
public final class DateTool {
    private static final SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");

    public static String getYMD(Date date){
        if (null == date){
            return "";
        }
        return ymd.format(date);
    }


    public static String getYMDHMS(Date date){
        if (null == date){
            return "";
        }
        return sd.format(date);
    }


    public static String dateToWeek(String datetime)  {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = (Date) f.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(datet);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        System.out.println(weekDays[w]);//星期二
        return weekDays[w];
    }



    private static final int FIRST_DAY = Calendar.MONDAY;

    public static void main(String[] args) {
        List<String> weekdays = getWeekdays();
        System.out.println(weekdays);
    }

    public static List<String> getWeekdays() {

        List<String> weekDaysList = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        setToFirstDay(calendar);
        for (int i = 0; i < 7; i++) {
            weekDaysList.add(printDay(calendar));
            calendar.add(Calendar.DATE, 1);
        }
        return weekDaysList;
    }

    private static void setToFirstDay(Calendar calendar) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            calendar.add(Calendar.DATE, -1);
        }
    }

    private static String printDay(Calendar calendar) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return  dateFormat.format(calendar.getTime());
    }
}
